var auctionMap = {
    'past_auction':1,
    'now_auction':2,
    'next_auction':3
};
var aution;
var init = false;
var container;
$(function () {
   aution = new Vue({
       el:'#auctionController',
       data:{
            auctionType:'now_auction',
            searchTime:'',
            auctions:[],
            now:new Date().format("yyyy-MM-dd"),
           nowNUm:''
       },
       watch:{
           searchTime:function (val) {
               this.reload();
           }
       },
       methods:{
            getAuctionClass:function (type) {
                return 'sort_item '+type+(type == this.auctionType?' active':'');
            },
           changeType:function (type) {
                var that = this;
                if (!that.auctionType || that.auctionType != type){
                    that.auctionType = type;
                    var now = new Date();
                    if (type == 'past_auction'){
                        if (that.isTwelve()){
                            now.setDate(now.getDate());
                        }else{
                            now.setDate(now.getDate() - 1);
                        }
                    }else if (type == 'next_auction'){
                        if (that.isTwelve()){
                            now.setDate(now.getDate() + 1);
                        }else {
                            now.setDate(now.getDate());
                        }
                    }else{
                        if (that.isTwelve()){
                            now.setDate(now.getDate()+1);
                        }else {
                            now.setDate(now.getDate());
                        }
                    }
                    if (that.searchTime != now.format("yyyy-MM-dd")){
                        that.searchTime = now.format("yyyy-MM-dd");
                    }else{
                        this.reload();
                    }

                }
           },
           getImg:function (index) {
               return sy.path+'/static/web/images/waterfull_0'+(index+1)+'.png';
           },
           reload:function(){
               this.auctions=[];
               container.masonry('destroy');
               this.init();
               this.loadAuctions();
           },
           loadAuctions:function () {
               var that = this;
               sy.postApi("/api/pawn/pawnList",{
                   token:_token,
                   type:auctionMap[that.auctionType],
                   searchTime:that.searchTime
               },function (data) {
                   that.auctions = data;
                   if (auctionMap[that.auctionType] == 2){
                       that.calSecond();
                   }
                   Vue.nextTick(function () {
                       var $newElems = $('.item').appendTo(container);
                       $newElems.imagesLoaded(function(){
                           $newElems.animate({ opacity: 1},800);
                           container.masonry( 'appended', $newElems,true);
                       });
                       // container.masonry( 'reload');
                   });
               })
           },
           calSecond:function () {
               if (auctionMap[this.auctionType] == 2){
                   var that = this;
                   $.each(that.auctions,function (i,o) {
                       if (o.pawnCountDown){
                           that.$set(o, 'time', DateUtil.formatSecondsToHMS(o.pawnCountDown--));
                       }
                   })
                   setTimeout(function(){
                       that.calSecond();
                   },1000)
               }

           },
           showTime:function (e) {
               calendar.show({ id: this.$refs.time })

           },
           jump:function (item) {
               location.href = sy.path+'/web/auction/'+item.goodsId;
           },
           time:function (type) {
           },
           getTimeNum:function () {
                var that = this;
               sy.postApi('/api/index/getPawnTime',{times:this.now},function (data) {
                   that.nowNUm = data;
               })
           },
           init:function () {
               tores();
               container = $('.waterfull ul');
               container.imagesLoaded(function(){
                   container.masonry({
                       columnWidth: 320,
                       itemSelector : '.item',
                       isFitWidth: true,//是否根据浏览器窗口大小自动适应默认false
                       isAnimated: true,//是否采用jquery动画进行重拍版
                       isRTL:false,//设置布局的排列方式，即：定位砖块时，是从左向右排列还是从右向左排列。默认值为false，即从左向右
                       isResizable: true,//是否自动布局默认true
                       animationOptions: {
                           duration: 800,
                           easing: 'easeInOutBack',//如果你引用了jQeasing这里就可以添加对应的动态动画效果，如果没引用删除这行，默认是匀速变化
                           queue: false//是否队列，从一点填充瀑布流
                       }
                   });
               });
           },
           isTwelve:function () {
               var dateArr = DateUtil.toArray();
               var hour = dateArr[3];
               return hour >= 12;
           }
       },
       created:function () {
           // this.changeType('now_auction');
           this.loadAuctions();
           this.getTimeNum();
       },
       mounted:function () {
           this.init();
           var that = this;

           var pastMax = -1;
           if (that.isTwelve()){
               pastMax = 0;
           }
           laydate.render({
               elem: '#select_date_past', //指定元素
               max:pastMax,
               trigger:'click',
               done: function(value){
                   that.searchTime = value;
               }
           });

           var nextMin = 0;
           if (that.isTwelve()){
               nextMin = 1;
           }
           laydate.render({
               elem: '#select_date_next', //指定元素
               max:7,
               min:nextMin,
               trigger:'click',
               done: function(value){
                   that.searchTime = value;
               }
           });
       },
       updated:function () {

       }
   });
});